<?php
/**
 * Extends the model class 
 */
uses('model');

/**
 * users 
 * 
 * @uses model
 * @package 
 * @version $id$
 * @copyright 2004-2006 Wiz Interactive
 * @author Jorge Pena <jmgpena@gmail.com> 
 * @license PHP Version 4.0 {@link http://www.php.net/license/4_0.txt}
 */
class users extends model
{
    var $_table = "users";
    var $_sql_create =
        'CREATE TABLE `users` (
            `id` INT NOT NULL ,
            `created_on` DATETIME NOT NULL ,
            `updated_on` DATETIME NOT NULL ,
            `username` VARCHAR( 255 ) NOT NULL ,
            `password` VARCHAR( 255 ) NOT NULL ,
            `email` VARCHAR( 255 ) NULL ,
            `name` VARCHAR( 255 ) NULL ,
            `confirmation_hash` VARCHAR( 255 ) NOT NULL ,
            `confirmed` TINYINT(1) NOT NULL default '0' ,
            `enabled` TINYINT(1) NOT NULL default '1' ,            
            PRIMARY KEY ( `id` )
        )';

    function create_password()
    {
        // Create the password
        $chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime()*1000000);
        $i = 0;
        $pass = '' ;

        while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $pass = $pass . $tmp;
            $i++;
        }

        $this->password = md5($pass);
        return $pass;
    }

    function set_password($password)
    {
        $this->password = md5($password);
    }

    function create_confirm_hash()
    {
        $confirm_hash = uniqid($this_class_name,true);
        $this->confirmation_hash = $confirm_hash;
        return $confirm_hash;
    }

    function read_from_post()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $this->username = $_POST['username'];
            if(isset($_POST['name']))
            {
                $this->name = $_POST['name'];
            }
            if(isset($_POST['password']))
            {
                $this->set_password($_POST['password']);
            }
            if(isset($_POST['email']))
            {
                $this->email = $_POST['email'];
            }
        }
    }

    function check_unique_username($username='')
    {
        if($username == '')
        {
            $username = $this->username;
        }
        $temp_users = $this->findFirst(array("conditions"=>"username = ".$this->quote($username)));

        return (!isset($temp_users));
    }

    function check_unique_email($email='')
    {
        if($email == '')
        {
            $email = $this->email;
        }
        $temp_users = $this->findFirst(array("conditions"=>"email = ".$this->quote($email)));

        return (!isset($temp_users));
    }

    /**
     * login 
     * 
     * This function checks if the user and password match and returns the
     * matched user object
     * 
     * @param mixed $username 
     * @param mixed $password 
     * @access public
     * @return user object or null
     */
    function login($username,$password)
    {
        //TODO
        $conditions = array("username = ".$this->_quote($username),
            "password=".$this->_quote(md5($password)));

        $temp_user = model::table('users')->findFirst(array("conditions"=>$conditions));

        return $temp_user;
    }

    function confirm($hash)
    {
        $user = model::table('users')->findFirst(array("conditions"=>"hash='".$hash."'"));
        if(isset($user))
        {
            $user->confirmed = 1;
            $user->save();
        }

        return $user;
    }

    function enable($id=null)
    {
        if(ctype_digit($id))
        {
            $user = model::table('users')->find($id);
        }
        elseif(is_a($this,'users'))
        {
            $user = $this;
        }
        else
        {
            trigger_error('Calling users::enable static with no id');
            return null;
        }
        if(isset($user))
        {
            $user->enabled = 1;
            $user->save();
        }
        return $user;
    }

    function disable($id=null)
    {
        if(ctype_digit($id))
        {
            $user = model::table('users')->find($id);
        }
        elseif(is_a($this,'users'))
        {
            $user = $this;
        }
        else
        {
            trigger_error('Calling users::enable static with no id');
            return null;
        }
        if(isset($user))
        {
            $user->enabled = 0;
            $user->save();
        }
        return $user;
    }
}
?>
